//
//  YYVisitorView.swift
//  YYWeibo
//
//  Created by strongwallyi on 15/12/28.
//  Copyright © 2015年 strongwallyi. All rights reserved.
//

import UIKit
import SnapKit

class YYVisitorView: UIView {

    //通过代码创建出来的 view 都会走 init frame 方法,所以外界也是可以调用的
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        startAnimation()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //供外界访问的 UI 设置方法
    func setupInfo(imgName: String?, messageText: String?){
        
        if imgName != nil{
            
            circleView.hidden = true
            iconView.image = UIImage(named: imgName!)
            messageLabel.text = messageText
        }else{
            startAnimation()
        }
    }

    
    //设置的动画
    private func startAnimation(){
        //初始化动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //执行一圈的时候
        anim.duration = 20
        //转圈
        anim.toValue = 2 * M_PI
        //重复次数
        anim.repeatCount = MAXFLOAT
        //切换界面. 或者 app 切到桌面上, cabasicanimation 会被释放
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    
    //懒加载各个控件
    //图标控件
    private lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //圆圈
    private lazy var circleView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //消息 label
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0
        label.textAlignment = .Center
        return label
    }()
    
    //注册按钮
    lazy var registerButton: UIButton = {
       let button = UIButton(textColor: UIColor.orangeColor(), fontSize: 15)
        button.setTitle("注册", forState: .Normal)
        //设置 button的背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return button
    }()
    
    //登陆按钮
    lazy var loginButton: UIButton = {
        let button = UIButton(textColor: UIColor.darkGrayColor(), fontSize: 15)
        button.setTitle("登陆", forState: .Normal)
        //设置button的背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return button
    }()
    
    ///半透明的那个阴影
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
 
}
    //给 visitorview 添加扩展
    extension YYVisitorView{
        private func setupUI(){
            backgroundColor = UIColor(white: 237/255, alpha: 1)

            //添加控件
            addSubview(circleView)
            addSubview(maskIconView)
            addSubview(iconView)
            addSubview(messageLabel)
            addSubview(registerButton)
            addSubview(loginButton)

//            //取消自动布局
//            iconView.translatesAutoresizingMaskIntoConstraints = false
//            //给 iconView 添加约束
//            addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//            addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))

            iconView.snp_makeConstraints { (make) -> Void in
                make.center.equalTo(snp_center)
            }

/*
            //给圆圈添加约束
            circleView.translatesAutoresizingMaskIntoConstraints = false
            addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
            addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
*/
            circleView.snp_makeConstraints { (make) -> Void in
                make.center.equalTo(iconView.snp_center)
            }

            //message 添加约束
//            messageLabel.translatesAutoresizingMaskIntoConstraints = false
//            addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//            addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
//            //约束宽度
//            addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))

            messageLabel.snp_makeConstraints { (make) -> Void in
                make.centerX.equalTo(iconView.snp_centerX)
                make.top.equalTo(circleView.snp_bottom).offset(16)
                make.width.equalTo(224)
            }

            //注册按钮添加约束
//            registerButton.translatesAutoresizingMaskIntoConstraints = false
//            addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
//            addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
//            addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))

            registerButton.snp_makeConstraints { (make) -> Void in
                make.leading.equalTo(messageLabel.snp_leading)
                make.top.equalTo(messageLabel.snp_bottom).offset(16)
                make.width.equalTo(100)
            }

            //登陆按钮添加约束
//            loginButton.translatesAutoresizingMaskIntoConstraints = false
//            addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
//            addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//            addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))

            loginButton.snp_makeConstraints { (make) -> Void in
                make.trailing.equalTo(messageLabel.snp_trailing)
                make.top.equalTo(registerButton.snp_top)
                make.width.equalTo(100)
            }


            //阴影添加约束
            // VFL: 可视化格式语言
            // | : 边界
            // H : 约束方向
            // metrics : 用于设置格式字符串里面的 `()` 里面的数值
            // views: 用于设置格式字符串里面的 `[]` 里面的View
            //利用 VHL 可视化语言进行约束
            //在 swift中使用可视化语言时,在 H:和 V:后面千万千万千万不要加空格,不然会出现约束失败的问题
//            maskIconView.translatesAutoresizingMaskIntoConstraints = false
//            addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: [], metrics: nil, views: ["view" : maskIconView]))
//            addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]-(offsetY)-[registerButton]", options: [], metrics: ["offsetY": -35], views: ["view": maskIconView, "registerButton": registerButton]))

            maskIconView.snp_makeConstraints { (make) -> Void in
                make.width.equalTo(snp_width)
                make.top.equalTo(snp_top)
                make.bottom.equalTo(registerButton.snp_bottom)
            }

        }
}






